package com.github.linkoog.model_designer.ide.editer.dbmodel.right_editer.table;

import com.github.linkoog.model_designer.ide.editer.dbmodel.domain.DatasourceNavigatable;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.ui.table.JBTable;
import com.linkoog.devtools.utils.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class DatasourceJTableModel extends AbstractTableModel implements Serializable {

    private static final Logger logger = Logger.getInstance(DbtableInfoJTableModel.class);

    private final List<String> columnList = Arrays.asList("名称","注释");

    private final List<DatasourceNavigatable> dataList = new ArrayList<>();

    private List<DatasourceNavigatable> tempDataList;


    public DatasourceJTableModel(){
    }

    public DatasourceJTableModel(Collection<DatasourceNavigatable> newRowList) {
        addRow(newRowList);
    }

    public void initTableColumn(JBTable jbTable){
        DefaultTableCellRenderer tableCellRenderer = new DefaultTableCellRenderer();
        tableCellRenderer.setHorizontalAlignment(SwingConstants.LEFT);
        tableCellRenderer.setBackground(Color.LIGHT_GRAY);



        int tableNameIndex = 0;
        TableColumn tableNameColumn = jbTable.getColumn(columnList.get(tableNameIndex));
        tableNameColumn.setMinWidth(88);
        tableNameColumn.setMaxWidth(88);
        tableNameColumn.setPreferredWidth(88);
        tableNameColumn.setCellRenderer(tableCellRenderer);


        int tableCommentIndex = 1;
        TableColumn tableCommentColumn = jbTable.getColumn(columnList.get(tableCommentIndex));
        /*tableCommentColumn.setMinWidth(65);
        tableCommentColumn.setMaxWidth(65);
        tableCommentColumn.setPreferredWidth(65);*/
        tableCommentColumn.setCellRenderer(tableCellRenderer);

    }


    @Override
    public int getRowCount() {
        return dataList.size();
    }

    @Override
    public int getColumnCount() {
        return columnList.size();
    }

    @Override
    public String getColumnName(int columnIndex) {
        /*if (columnIndex == 4){
            return "";
        }*/
        return columnList.get(columnIndex);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        DatasourceNavigatable datasourceNavigatable = dataList.get(rowIndex);
        switch (columnIndex){
            case 0: return datasourceNavigatable.getName();
            case 1: return datasourceNavigatable.getComment();
            //case 2: return datasource.isChecked();
        }
        return null;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        switch (columnIndex){
            case 1:  return false;
        }
        return false;
    }

    @Override
    public Class getColumnClass(int c)	{
        Object value = getValueAt(1, c);
        if(value != null){
            return value.getClass();
        } else {
            return super.getClass();
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        DatasourceNavigatable datasourceNavigatable = dataList.get(rowIndex);
        switch (columnIndex){
            case 1:
                break;
        }
    }

    public final boolean hasElements() {
        return dataList.size() > 0;
    }


    public final void addRow(@NotNull DatasourceNavigatable newRow) {
        dataList.add(newRow);
        super.fireTableDataChanged();
    }

    public final void addRow(@NotNull Collection<DatasourceNavigatable> newRowList) {
        tempDataList = null;
        if (newRowList != null && !newRowList.isEmpty()){
            dataList.addAll(newRowList);
        }
        super.fireTableDataChanged();
    }

    public final void removeRow(int rowIndex) {
        dataList.remove(rowIndex);
        super.fireTableDataChanged();
    }

    public final void rowCheck(int rowIndex) {
        DatasourceNavigatable datasourceNavigatable = dataList.get(rowIndex);
        //datasource.setChecked(!datasource.isChecked());
        super.fireTableDataChanged();
    }

    public final void clear() {
        dataList.clear();
        super.fireTableDataChanged();
    }

    public final void reset(@NotNull Collection<DatasourceNavigatable> newRowList) {
        dataList.clear();
        tempDataList = null;
        if (newRowList != null && !newRowList.isEmpty()){
            dataList.addAll(newRowList);
        }
        super.fireTableDataChanged();
    }


    public final DatasourceNavigatable getRowData(int rowIndex) {
        return dataList.get(rowIndex);
    }

    public List<String> getColumnList() {
        return columnList;
    }

    /*public List<TableInfo> getCheckedRow(){
        return dataList.stream()
                .filter(tableInfo -> tableInfo.isChecked())
                .map(TableInfo::deepCopyWithCheckColumn)
                .collect(Collectors.toList());
    }*/




    public void filter(String tableName){
        if (tempDataList == null){
            tempDataList = new ArrayList<>(dataList);
        }


        if (tempDataList.size() == 0){
            return;
        }

        List<DatasourceNavigatable> list;
        if (StringUtils.isNotBlank(tableName)){
            list = tempDataList.stream()
                    .filter(tableInfo -> tableInfo.getName().contains(tableName))
                    .collect(Collectors.toList());
        } else {
            list = tempDataList;
        }

        dataList.clear();
        dataList.addAll(list);
        super.fireTableDataChanged();
    }

    public List<DatasourceNavigatable> getAllDataList() {
        if (tempDataList == null){
            return dataList;
        } else {
            return tempDataList;
        }
    }
}
